package com.starbaba.creditcard.view;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.creditcard.model.CreditCardUserInfo;
import com.starbaba.creditcard.service.CreditCardUserInfoService;
import com.starbaba.stat.model.LoanBackCore;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;


@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class CreditCardUserInfoAction extends BaseAction implements ModelDriven<LoanBackCore>{

	private CreditCardUserInfoService service;
	private InputStream inputStream;   //导出文件输入流
	private String fileName;           //导出文件名
	
	@Autowired
	@Qualifier("creditCardUserInfoService")
	public void setService(CreditCardUserInfoService service) {
		this.service = service;
	}
	
	public String showPage() throws Exception{
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		service.showPage(paramsMap);
		
		return "success";
	}
	
	/**
	 * 查询列表数据
	 */
	public void queryList(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		try {
			List<CreditCardUserInfo> list = new ArrayList<CreditCardUserInfo>();
			list = service.getList(start, length, pageResponse, paramsMap);
            pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 导出列表数据
	 */
	public String exportList(){
		
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
        String result = service.exportList(paramsMap);
        try {
			  inputStream = new ByteArrayInputStream(result.getBytes("GBK"));
			  fileName = "credit_card_user_info_export_" + System.currentTimeMillis() + ".csv";
		} catch (UnsupportedEncodingException e) {
			LogService.error(e);
			return ERROR;
		}
		return SUCCESS;
		
		
	}
	
	@Override
	public LoanBackCore getModel() {
		
		return null;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
}
